package com.haizhi.workstation.listener;

import com.haizhi.workstation.mq.MQSender;
import com.haizhi.workstation.util.Constants;
import org.apache.commons.io.filefilter.FileFilterUtils;
import org.apache.commons.io.filefilter.RegexFileFilter;
import org.apache.commons.io.monitor.FileAlterationMonitor;
import org.apache.commons.io.monitor.FileAlterationObserver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.nio.file.Paths;
import java.util.concurrent.TimeUnit;

/**
 * 在服务启动的时候启动文件监听器
 */
@Component
public class InitFileListener implements ApplicationListener<ContextRefreshedEvent> {
    @Autowired
    private MQSender sender;
    @Autowired
    private Constants constants;
    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        new Thread(){
            public void run() {
                FileWatcher fileWatcher = new FileWatcher(Paths.get(constants.PUBLIC_RECEIVE_FOLDER),
                        new RegexFileFilter(".*?[0-9]{10,20}.json"),new MyListener(sender));
                fileWatcher.watch();
            }
        }.start();
    }

}

